<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_daftar_ulang extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($dt){
		$this->db->flush_cache();
		$this->db->select('a.PMBPeriodID, a.no_pendaftaran, a.nama, a.kota, a.kd_prodi, b.nama_prodi as nama_prodi, a.alamat, a.no_telp, a.nama_ortu, a.alamat_ortu, a.pekerjaan');
		$this->db->from('pmb_seleksi a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->where('a.PMBPeriodID', $dt['PMBPeriodID']);
		$this->db->where('a.status', 'Diterima');
		$this->db->limit($dt['jumlah'], 0);
		$this->db->order_by('a.no_pendaftaran', 'ASC');
		
		$q = $this->db->get()->result();
		
		$out = '';
		$i=1;
		$color = '';
		foreach($q as $r){
			$color = ($i % 2 == 0)?'#FFFFFF':'#fff9cf';
			$out .= '<tr>';
			$out .= '  <td bgcolor="'.$color.'">'.$i;
			$out .= '     <input type="hidden" name="data['.$i.'][PMBPeriodID]" value="'.$r->PMBPeriodID.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][no_pendaftaran]" value="'.$r->no_pendaftaran.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][nama]" value="'.$r->nama.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][kota]" value="'.$r->kota.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][alamat]" value="'.$r->alamat.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][no_telp]" value="'.$r->no_telp.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][kd_prodi]" value="'.$r->kd_prodi.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][nama_ortu]" value="'.$r->nama_ortu.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][alamat_ortu]" value="'.$r->alamat_ortu.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][pekerjaan]" value="'.$r->pekerjaan.'">';
			$out .= '  </td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->nama.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->alamat.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->no_telp.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->kd_prodi.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->nama_ortu.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->alamat_ortu.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->pekerjaan.'</td>';
			$out .= '  <td bgcolor="'.$color.'">';
			$out .= '    <label><input type="text" name="data['.$i.'][nim]" value=""> </label>';
			$out .= '  </td>';
			$out .= '</tr>';
			$i++;
		}
		
		return $out;
	}
	
	function InsertOnDB($data){
		$this->db->trans_start();
		
		foreach($data as $row){
			if($row['nim'] != '' || $row['nim'] != null){
				# insert to table mahasiswa
				$this->db->flush_cache();
				$this->db->set('thn_angkatan', date('Y'));
				$this->db->set('nim', $row['nim']);
				$this->db->set('nama', $row['nama']);
				$this->db->set('kota', $row['kota']);
				$this->db->set('alamat', $row['alamat']);
				$this->db->set('no_telp', $row['no_telp']);
				$this->db->set('kd_prodi', $row['kd_prodi']);
				$this->db->set('nama_ortu', $row['nama_ortu']);
				$this->db->set('alamat_ortu', $row['alamat_ortu']);
				$this->db->set('pekerjaan', $row['pekerjaan']);
				$this->db->set('status', 'Mahasiswa Terdaftar');
				
				$this->db->set('CUID', $this->session->userdata('user_id'));
				$this->db->set('CDATE', date('Y-m-d H:i:s'));
				
				$this->db->insert('tbl_mahasiswa');
				
				# delete di tabel seleksi
				$this->db->flush_cache();
				$this->db->set('status', 'Sudah Daftar Ulang');
				$this->db->where('PMBPeriodID', $row['PMBPeriodID']);
				$this->db->where('no_pendaftaran', $row['no_pendaftaran']);
				//$this->db->delete('pmb_seleksi');
				$this->db->update('pmb_seleksi');
			}
		}
		
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
}

?>